package com.scap.authen.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.scap.authen.entity.Role;

@Repository("voRoleDao")
public class RoleDaoImpl extends AbstractDao implements RoleDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllRole() {			
			Criteria criteria = getSession().createCriteria(Role.class);
		return (List<Object>)criteria.list();
	}

	@Override
	public boolean insertRole(Role poRole) {
		boolean result = false;		
		try {
			persist(poRole);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}

	@Override
	public boolean updateSite(Role poRole) {
		boolean result = false;
		
		try {
			
			Query query = getSession().createQuery("Update Role set vaRoleName = :vaRoleName"
					+ ", vaStatus = :vaStatus "
					+ ", vaSiteId = :vaSiteId "
    				+ " Where vaRoleId = :vaRoleId");
			
			query.setString("vaRoleName", poRole.getVaRoleName());
			query.setString("vaStatus", poRole.getVaStatus());
			query.setString("vaSiteId", poRole.getVaSiteId());
			query.setString("vaRoleId", poRole.getVaRoleId());
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}
	
	@Override
	public boolean updateAppStatus(String vaRoleId, String vaSiteId) {
		boolean result = false;		
		try {		
			Query query = getSession().createQuery("Update Role set vaAppStatus = 'Y' "
    				+ " Where vaRoleId =:vaRoleId AND vaSiteId =:vaSiteId");
			
			query.setString("vaRoleId", vaRoleId);
			query.setString("vaSiteId", vaSiteId);
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}
	
	@Override
	public boolean updateMenuStatus(String vaRoleId, String vaSiteId) {
		boolean result = false;		
		try {		
			Query query = getSession().createQuery("Update Role set vaMenuStatus = 'Y' "
    				+ " Where vaRoleId =:vaRoleId AND vaSiteId =:vaSiteId");
			
			query.setString("vaRoleId", vaRoleId);
			query.setString("vaSiteId", vaSiteId);
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getRoleList() {
		Query query = getSession().createQuery("select role.vaRoleId , role.vaRoleName"
				+ " from Role as role ");	
		return (List<Object[]>)query.list();
	}
}
